﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Data;
using System.Windows.Interop;
using System.Windows;
using System.Runtime.InteropServices;
//using vbAccelerator.Components.ImageList;
using System.Diagnostics;
using System.Threading;
using System.Windows.Threading;
using System.Globalization;

namespace AssignmentsManage.Converters
{
    public class FileToIconConverter : DependencyObject, IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null && value.GetType() == typeof(DataClasses.LinkedFile))
            {
                DataClasses.LinkedFile file = value as DataClasses.LinkedFile;
                string finalPath;

                System.Windows.Media.ImageSource icon;

                if (file.IsURL)
                {
                    finalPath = RegLib.RegistryHelper.getDefaultWebBrowser();
                }
                else
                {
                    finalPath = file.FilePath;
                }

                if (System.IO.File.Exists(finalPath))
                {
                    using (System.Drawing.Icon sysicon = System.Drawing.Icon.ExtractAssociatedIcon(finalPath))
                    {
                        icon = System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(
                                  sysicon.Handle,
                                  System.Windows.Int32Rect.Empty,
                                  System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
                    }
                }
                else
                {
                    icon = new BitmapImage(new Uri(@"pack://application:,,,/AssignmentsManage;component/Images/file_document_paper_red_g37966.png"));
                }

                return icon;
            }

            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException("MethodToValueConverter can only be used for one way conversion.");
        }
    }
}
